﻿using System;
using System.IO;
using System.Reflection;
using Sfd.Entities;
using Sfd.Repositories;
using Sfd.Repositories.Files;

namespace Sfd.Services
{
    public class AzureUserService
    {
        private readonly IAzureUserRepository _repository;

        #region ctor
        public AzureUserService()
        {
            FileName = Path.Combine(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                "userInfo.xml");
            _repository = new AzureUserDataContractRepository(FileName);
        }

        public AzureUserService(IAzureUserRepository repository)
        {
            _repository = repository;
        } 
        #endregion

        public string FileName { get; private set; }

        public AzureUser LoadUser()
        {
            // création du user s'il n'existe pas
            if (!File.Exists(FileName))
                Save(new AzureUser());

            return _repository.LoadUser();
        }

        public void Save(AzureUser user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            _repository.Save(user);
        }
    }
}
